package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.KKutil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink15_Watermark_Sideout_2 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(1);

        SingleOutputStreamOperator<String> mainStream = env
                .socketTextStream("hadoop162", 9999)
                .map(line -> {
                    String[] strings = line.split(",");
                    return new WaterSensor(strings[0], Long.valueOf(strings[1]), Integer.valueOf(strings[2]));
                })
                .process(new ProcessFunction<WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        //sensor1放主流  sensor2 放侧输出流
                        if ("sensor_1".equals(value.getId())) {
                            out.collect(value.toString());
                        } else if ("sensor_2".equals(value.getId())) {
                            ctx.output(new OutputTag<String>("s22"){}, value.toString());
                        }
                    }
                });
        mainStream.print("s1");
        mainStream.getSideOutput(new OutputTag<String>("s22"){}).print("s2");

        env.execute();
    }
/*
*       flink 如何做到数据不丢失
*       1.事件时间 + 水印
*       2，允许迟到
*       3.侧输出流
*
* */


}
